package zwp.exercise.intereview.base.binary_search;

/**
 * @Author zwp14
 * @create 2024/6/28 12:02
 * @description 二分查找复习1
 */
public class BinarySearchReview1 {
    public static void main(String[] args) {
        //定义数组
        int[] array = {1, 5, 8, 11, 19, 22, 31, 35, 40, 45, 48, 49, 50};
        int start = 0;
        int end = array.length-1;
        int target = 47;
        int i = binarySearchMethod(array, start, end, target);
        if (i == -1){
            System.out.println("查找数字不在当前数组中!");
        }
    }

    private static int binarySearchMethod(int[] array, int start, int end, int target) {
        while (start <= end){
            int half = (start + end) >>> 1;
            if (array[half]> target){
                end = half -1;
            }else if (array[half] < target){
                start = half +1;
            }else if (array[half] == target){
                System.out.println("查找到数字:"+array[half]+"索引:"+half);
                return half;
            }
        }
        return -1;
    }
}
